from pyspades.server import orientation_data, grenade_packet, block_action
from pyspades.common import coordinates, Vertex3
from pyspades.constants import *
from pyspades.world import Grenade

def apply_script(protocol, connection, config):
    class OhNoOhGodConnection(connection):
        holecount = 0
        holing = False
        hole_xo = 0.0
        hole_yo = 0.0
        hole_zo = 0.0
        hole_x = 0.0
        hole_y = 0.0
        hole_z = 0.0
        hole_orient_x = 0.0
        hole_orient_y = 0.0
        hole_orient_z = 0.0
        hole_build = 1

        def update_holemaker(self):
            if self.name is None or self.holing == False:
                return
            if self.holecount > 150:
                self.holecount = 0
                self.holing = False
                return
            self.hole_xo += self.hole_orient_x
            self.hole_yo += self.hole_orient_y
            self.hole_zo += self.hole_orient_z
            self.hole_x = int(self.hole_xo)
            self.hole_y = int(self.hole_yo)
            self.hole_z = int(self.hole_zo)
            map = self.protocol.map
            if self.hole_x < 0 or self.hole_x > 512 or self.hole_y < 0 or self.hole_y > 512 or self.hole_z < 0 or self.hole_z > 63:
                self.holecount = 0
                self.holing = False
                return
            if self.hole_build == 1:
                block_action.x = self.hole_x
                block_action.y = self.hole_y
                block_action.z = self.hole_z
                block_action.value = 0
                self.protocol.send_contained(block_action, save = True)
                map.set_point(self.hole_x, self.hole_y, self.hole_z, self.color + (255,), user = False)
                self.holecount += 1
                return
            if self.on_block_destroy(self.hole_x, self.hole_y, self.hole_z, GRENADE_DESTROY) == False:
                return

            for nade_x in xrange(self.hole_x - 1, self.hole_x + 2):
                for nade_y in xrange(self.hole_y - 1, self.hole_y + 2):
                    for nade_z in xrange(self.hole_z - 1, self.hole_z + 2):
                        if map.remove_point(nade_x, nade_y, nade_z):
                            self.on_block_removed(nade_x, nade_y, nade_z)
            block_action.x = self.hole_x
            block_action.y = self.hole_y
            block_action.z = self.hole_z
            block_action.value = GRENADE_DESTROY
            block_action.player_id = self.player_id
            self.protocol.send_contained(block_action, save = True)
            self.holecount += 1

        def on_shoot_set(self, fire):
            if self.weapon == SHOTGUN_WEAPON or self.tool != WEAPON_TOOL:
                return connection.on_shoot_set(self, fire)
            if fire == True:
                grenade_packet.value = 0
            else:
                return connection.on_shoot_set(self, fire)
            self.hole_xo = self.world_object.position.x
            self.hole_yo = self.world_object.position.y
            self.hole_zo = self.world_object.position.z
            self.hole_orient_x = self.world_object.orientation.x
            self.hole_orient_y = self.world_object.orientation.y
            self.hole_orient_z = self.world_object.orientation.z
            self.holecount = 0
            self.holing = True
            self.hole_build = self.weapon
            self.update_holemaker()
            connection.on_shoot_set(self, fire)

        def on_block_build(self, x, y, z):
            if self.weapon != RIFLE_WEAPON:
                return connection.on_block_build(self, x, y, z)
            self.hole_xo = x
            self.hole_yo = y
            self.hole_zo = z
            self.hole_orient_x = self.world_object.orientation.x
            self.hole_orient_y = self.world_object.orientation.y
            self.hole_orient_z = self.world_object.orientation.z
            self.holecount = 0
            self.holing = True
            self.hole_build = 1
            self.update_holemaker()
            connection.on_block_build(self, x, y, z)


    class OhNoOhGodProtocol(protocol):      
        ohno_second_counter = 0
        def on_world_update(self):
            if self.ohno_second_counter >= 5:
                for player in self.players.values():
                    player.update_holemaker()
                self.ohno_second_counter = 0
            else:
                self.ohno_second_counter += 1
            protocol.on_world_update(self)
    return OhNoOhGodProtocol, OhNoOhGodConnection

